/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_TASK_WORKER_H
#define IMAGE_KNIFE_C_TASK_WORKER_H

#include "task_worker.h"


namespace ImageKnifePro {

class TaskWorkerNapi : public TaskWorker {
public:
    bool PushTask(AsyncFunc execute, AsyncFunc complete, void *data, std::string taskInfo = "",
                  ImageKnifeQOS qos = ImageKnifeQOS::DEFAULT) override;

    bool PushTask(AsyncTask execute, AsyncTask complete, std::shared_ptr<ImageKnifeTaskInternal> task,
                  ImageKnifeQOS qos = ImageKnifeQOS::DEFAULT) override;

    void ToMainThread(std::function<void(void*)> func, void* data) override;

    ~TaskWorkerNapi() override
    {
        if (threadsafeFunc_ != nullptr) {
            napi_release_threadsafe_function(threadsafeFunc_, napi_tsfn_release);
        }
    }
private:
    class InnerWorker {
    public:
        napi_async_work work = nullptr;
        napi_qos_t qos = napi_qos_default;
        std::string taskInfo;

        std::shared_ptr<ImageKnifeTaskInternal> task = nullptr;
        AsyncTask executeTask = nullptr;
        AsyncTask completeTask = nullptr;

        void *data = nullptr;
        AsyncFunc execute = nullptr;
        AsyncFunc complete = nullptr;
        std::function<void(void*)> mainThreadFunc = nullptr;

        static void ExecuteCallback(napi_env env, void *usrdata)
        {
            IMAGE_KNIFE_CHECK(usrdata == nullptr);
            InnerWorker* worker = static_cast<InnerWorker*>(usrdata);
            if (worker->executeTask != nullptr) {
                worker->executeTask(worker->task);
            } else if (worker->execute != nullptr) {
                worker->execute(worker->data);
            }
        }

        static void CompleteCallback(napi_env env, napi_status status, void *usrdata)
        {
            IMAGE_KNIFE_CHECK(usrdata == nullptr);
            InnerWorker* worker = static_cast<InnerWorker*>(usrdata);
            if (status != napi_cancelled) {
                if (worker->completeTask != nullptr) {
                    worker->completeTask(worker->task);
                } else if (worker->complete != nullptr) {
                    worker->complete(worker->data);
                }
            }

            napi_delete_async_work(env, worker->work);
            delete worker;
        }
    };

    napi_threadsafe_function threadsafeFunc_ = nullptr;

    void InitNapiEnv() override;

    bool QueueWork(InnerWorker *worker);

    bool QueueWorkThreadsafe(InnerWorker *worker);

    static void CallJsCallback(napi_env env, napi_value jsCallback, void *context, void* usrdata);

    void PrepareThreadsafeFunc();
};

} // end of namespace

#endif // IMAGE_KNIFE_C_TASK_WORKER_H
